/*
 * @Author: wwssaabb
 * @Date: 2021-07-16 16:57:22
 * @LastEditTime: 2021-07-17 14:45:42
 * @FilePath: \demo\koa2_demo\demo\ajax文件上传进度条展示\server.js
 */

const koa = require('koa')
const path = require('path')
const koaBody = require('koa-body')
const cors = require('koa2-cors')
const router = require('koa-router')()

const app=new koa()

app.use(koaBody({
  multipart:true,
  formidable:{
    uploadDir:path.join(__dirname,'/uploads'), // 设置文件上传目录
    keepExtensions: true,    // 保持文件的后缀
    maxFileSize:200 * 1024 * 10224
  }
}))

app.use(cors())

router.post('/upload',async (ctx,next)=>{
  const request=ctx.request
  console.log(path.join(__dirname,'/uploads'))
  console.log(request.files)
  ctx.status=200
  ctx.body=request.files
  /* ctx.status=200
  ctx.body='上传完成' */
  //await next()
})

app.use(router.routes())
app.use(router.allowedMethods())

app.listen(3000,err=>{
  if(!err){
    console.log('服务器开启成功')
  }else{
    console.log('服务器开启失败'+err)
  }
})